<template>
<div style="width: 500px">
  <el-input
      style="margin-bottom: 10px;"
      size="small"
      prefix-icon="el-icon-search"
      placeholder="请输入部门名称进行搜索"
      v-model="filterText">
  </el-input>

  <el-tree
      class="filter-tree"
      :data="deps"
      :props="defaultProps"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      ref="tree">
    <span  style="width:100%;display:flex;justify-content: space-between" slot-scope="{ node, data }">
        <span style="font-size: 16px">{{ node.label }}</span>
        <span>
          <el-button
              class="depButton"
              type="primary" icon="el-icon-plus"
              size="mini"
              @click="() => showAppend(data)">
          </el-button>
          <el-button
              class="depButton"
              type="danger" icon="el-icon-minus"
              size="mini"
              @click="() => Remove(data)">
          </el-button>
        </span>
      </span>
  </el-tree>
  <el-dialog
      title="添加部门"
      :visible.sync="dialogVisible"
      width="30%">
    <div>
      <div>
        <el-tag type="info" size="medium" style="margin-right: 5px">上级部门</el-tag>
        <span>{{this.pname}}</span>
      </div>
      <div style="margin-top: 5px">
        <el-tag type="info" size="medium">部门名称</el-tag>
        <el-input v-model="dep.name" placeholder="请输入部门名称" style="width: 200px;margin-left: 5px" size="mini"></el-input>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="append">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "DepManager",
  data() {
    return {
      filterText: '',
      dialogVisible:false,
      dep:{
        name:'',
        parentId:-1
      },
      pname:'',
      deps: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  mounted() {
    this.initDeps();
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    initDep(){
      this.dep={
        name : '',
        parentId: -1
      }
      this.pname=''
    },
    initDeps(){
      this.getRequest("system/basic/department/").then(resp=>{
        this.deps=resp;
      })
    },
    showAppend(data){
      this.pname = data.name;
      this.dep.parentId = data.id;
      this.dialogVisible = true;
    },
    append(){
      this.postRequest("system/basic/department/",this.dep).then(resp=>{
        if(resp){
          // this.initDeps();
          this.depToDeps(null,this.deps,resp.obj);

        }
        this.initDep();
        this.dialogVisible=false;
      })
    },
    depToDeps(p,deps,dep){
      for(let i = 0;i<deps.length;i++){
        let d = deps[i];
        if(d.id=dep.parentId){
          d.children=d.children.concat(dep);
          if(d.children.length>0){
            p.parent = true;
          }
          return;
        }else{
          this.depToDeps(d,d.children,dep);
        }
      }
    },
    Remove(data){
        this.$confirm('此操作将永久删除'+data.name+', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         this.deleteRequest("system/basic/department/"+data.id).then(resp=>{
           if(resp){
             this.removeDepFromDeps(null,this.deps,data.id);
           }
         })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    removeDepFromDeps(p,deps,id){
      for(let i=0;i<deps.length;i++){
        let d = deps[i];
        if(d.id==id){
          deps.splice(  i,1)
          if(deps.length==0){
            p.parent = false;
          }
          return
        }else {
          this.removeDepFromDeps(d,d.children, id);
        }
      }
    }
  }
}
</script>

<style>
.depButton{
  padding: 1px;
}
</style>